package com.hy.service.impl;

import com.hy.Vo.PurchaseApplyDeptUserVo;
import com.hy.bean.PurchaseApply;
import com.hy.bean.PurchaseApplyDetail;
import com.hy.bean.RespBean;
import com.hy.dao.PurchaseApplyDao;
import com.hy.dao.PurchaseApplyDetailDao;
import com.hy.dao.impl.PurchaseAppIyDaoImpl;
import com.hy.dao.impl.PurchaseApplyDetailDaoImpl;
import com.hy.service.PurchaseApplyService;
import com.hy.utils.DBManager;
import org.apache.commons.dbutils.DbUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * (PurchaseApply)表服务实现类
 *
 * @author makejava
 * @since 2024-04-29 14:47:14
 */
public class PurchaseApplyServiceImpl implements PurchaseApplyService {
    private PurchaseApplyDao purchaseApplyDao = new PurchaseAppIyDaoImpl();
    private PurchaseApplyDetailDao purchaseApplyDetailDao = new PurchaseApplyDetailDaoImpl();

    /**
     * 通过ID查询单条数据
     *
     * @param id
     * @return 实例对象
     */
    @Override
    public PurchaseApply queryById(int id) {
        return this.purchaseApplyDao.queryById();
    }

    @Override
    public PurchaseApply queryByPaId(Integer id){
        return purchaseApplyDao.queryByPaId(id);
    }

    @Override
    public List<PurchaseApplyDeptUserVo> queryAll(String deptName,String username){
        return purchaseApplyDao.queryMap(deptName, username);
    }

    /**
     * 新增数据
     *
     * @param purchaseApply 实例对象
     * @return 实例对象
     */
    @Override
    public RespBean insert(PurchaseApply purchaseApply) {
        int insert = purchaseApplyDao.insert(purchaseApply);
        if (insert > 0){
            return new RespBean().success("添加成功");
        }else {
            return new RespBean().error("添加失败");
        }
    }

    /**
     * 修改数据
     *
     * @param purchaseApply 实例对象
     * @return 实例对象
     */
    @Override
    public RespBean update(PurchaseApply purchaseApply) {
        int update = purchaseApplyDao.update(purchaseApply);
        if (update > 0){
            return new RespBean().success("修改成功");
        }else {
            return new RespBean().error("修改失败");
        }
    }

    @Override
    public RespBean updateStatus(Integer id,Integer status){
        Connection connection = null;
        try {
            connection = DBManager.getConnection();
            int update = purchaseApplyDao.updateStatus(connection,id, status);

            if (update > 0){
                return new RespBean().success("修改成功");
            }else {
                return new RespBean().error("修改失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }

    /**
     * 通过主键删除数据
     *
     * @param id
     * @return 是否成功
     */
    @Override
    public RespBean deleteById(Integer id) {
        Connection connection = null;
        try {
            connection = DBManager.getConnection();
            connection.setAutoCommit(false);
            int i1 = 0;
            int i = purchaseApplyDao.deleteById(connection, id);
            List<PurchaseApplyDetail> purchaseApplyDetails = purchaseApplyDetailDao.queryByPurchaseApplyId(connection, id);
            for (PurchaseApplyDetail purchaseApplyDetail : purchaseApplyDetails) {
                i1 = purchaseApplyDetailDao.deleteByIds(connection, purchaseApplyDetail.getId());
            }
            if (i > 0 && i1 > 0){
                connection.commit();
                return new RespBean().success("删除成功");
            }else {
                connection.rollback();
                return new RespBean().error("删除失败");
            }
        } catch (Exception e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
